package com.quarantine.web;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.quarantine.model.member;
import com.quarantine.service.MemberManager;
import com.quarantine.web.CrudActionSupport;

@Namespace("/base")
@Results({
	@Result(name ="save", location = "member!list.action", type ="redirect",params={"currentpage","${currentpage}","sort","${sort}"}),
})
public class MemberAction extends CrudActionSupport<member> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private member entity;
	private MemberManager member;
	private String id;
	private List<member> memberList;
	private String sort;
	/**
	 * 分頁用
	 */
	private int currentpage;
	private int pageNo;
	public static final int pageSize = 20;
	private int prePage;
	private long totalPages;
	private int nextPage;
	private long allsize;
	@Override
	public member getModel() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String list() throws Exception {
		int pageSize = 6;
		String category = null;
		Page<member> page = new Page<member>();
		page.setAutoCount(true);
		page.setPageNo(currentpage);
		page.setPageSize(pageSize);
		System.out.println(sort);
		if(sort.equals("1")){     //副会长单位、常务理事单位、理事会单位、一般会员单位
		       category="副会长单位";
		      sort="1";
		      member.getList(page,category);
		      setMemberList(page.getResult());
				pageNo = page.getPageNo();
				prePage = page.getPrePage();
				nextPage = page.getNextPage();
				totalPages = page.getTotalPages();
				if(totalPages==0){
					
					pageNo=0;
				}
				allsize = page.getTotalCount();
				return "list1";  
		}
		if(sort.equals("2")){
			  category="常务理事单位";
            sort="2";
            member.getList(page,category);
            setMemberList(page.getResult());
    		pageNo = page.getPageNo();
    		prePage = page.getPrePage();
    		nextPage = page.getNextPage();
    		totalPages = page.getTotalPages();
            if(totalPages==0){
				
				pageNo=0;
			}
    		allsize = page.getTotalCount();
    		return "list2";  
		}
		if(sort.equals("3")){
			  category="理事单位";
			  sort="3";
			  member.getList(page,category);
			  setMemberList(page.getResult());
				pageNo = page.getPageNo();
				prePage = page.getPrePage();
				nextPage = page.getNextPage();
				totalPages = page.getTotalPages();
				if(totalPages==0){
					
					pageNo=0;
				}
				allsize = page.getTotalCount();
				return "list3";  
		}
		if(sort.equals("4")){
			  category="一般会员单位";
			  sort="4";
			  member.getList(page,category);
			  setMemberList(page.getResult());
				pageNo = page.getPageNo();
				prePage = page.getPrePage();
				nextPage = page.getNextPage();
				totalPages = page.getTotalPages();
				if(totalPages==0){
					
					pageNo=0;
				}
				allsize = page.getTotalCount();
				return "list4";  
		}
		
			member.getall(page);
			sort="5";
			setMemberList(page.getResult());
			pageNo = page.getPageNo();
			prePage = page.getPrePage();
			nextPage = page.getNextPage();
			totalPages = page.getTotalPages();
            if(totalPages==0){
				
				pageNo=0;
			}
			allsize = page.getTotalCount();
			return "list5";  
		
	                                        //加入类别来返回各个类别的列表
	}
	public String show() {
		entity = member.getById(id);
		if(entity.getCategory().equals("副会长单位")){     //副会长单位、常务理事单位、理事会单位、一般会员单位
		      sort="1";
		}
		if(entity.getCategory().equals("常务理事单位")){
                sort="2";
		}
		if(entity.getCategory().equals("理事单位")){
			  sort="3";
		}
		if(entity.getCategory().equals("一般会员单位")){
			  sort="4";
		}
		if(!entity.getCategory().equals("副会长单位")&&!entity.getCategory().equals("常务理事单位")&&!entity.getCategory().equals("理事单位")&&!entity.getCategory().equals("一般会员单位")){
			sort="5";
		}
		return "details";
	}
	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	 

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entity = new member();
		} else {
			entity = member.getById(id);
		}
		
	}

	public member getEntity() {
		return entity;
	}

	public void setEntity(member entity) {
		this.entity = entity;
	}

	public MemberManager getMember() {
		return member;
	}
	@Autowired
	public void setMember(MemberManager member) {
		this.member = member;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<member> getMemberList() {
		return memberList;
	}

	public void setMemberList(List<member> memberList) {
		this.memberList = memberList;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public long getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(long totalPages) {
		this.totalPages = totalPages;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public long getAllsize() {
		return allsize;
	}

	public void setAllsize(long allsize) {
		this.allsize = allsize;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

}
